package com.zhdl.modules.demo.web.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.RandomUtil;
import com.zhdl.common.util.WebResult;
import com.zhdl.modules.demo.entity.TestDemo;
import com.zhdl.modules.demo.service.TestDemoService;
import com.zhdl.modules.demo.service.dto.DemoTestCriteria;
import com.zhdl.modules.demo.web.request.DemoTest;
import com.zhdl.modules.demo.web.request.DemoTestRequest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 接口文档测试
 *
 * @Author: zhangyy
 * @Email: zhang10092009@hotmail.com
 * @Date: 2023/3/29 9:30:02
 * @Version: v1.0
 * @Modified: 示例测试
 * @Description:
 */
@RequestMapping(path = "/api/demo", produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
public class DemoController {

    @Resource
    private TestDemoService testDemoService;

    /**
     * 测试提交校验
     *
     * @param test
     * @return
     */
    @PostMapping("/send")
    public WebResult<Object> post(@RequestBody @Validated DemoTest test) {
        return WebResult.okResult(test);
    }

    /**
     * 保存
     *
     * @return
     */
    @GetMapping("/save")
    public WebResult<TestDemo> insert() {
        TestDemo testDemo = new TestDemo();
        testDemo.setId(RandomUtil.randomNumbers(6));
        testDemo.setName(RandomUtil.randomString(4));
        testDemo.setAddress(RandomUtil.randomString(6));
        return WebResult.okResult(testDemoService.insert(testDemo));
    }

    /**
     * 测试保存关联表数据
     *
     * @return
     */
    @GetMapping("/saveDesc")
    public WebResult<TestDemo> saveDesc() {
        TestDemo testDemo = new TestDemo();
        testDemo.setId(RandomUtil.randomNumbers(6));
        testDemo.setName(RandomUtil.randomString(4));
        testDemo.setAddress(RandomUtil.randomString(6));
        testDemoService.saveDesc(testDemo);
        return WebResult.okResult(testDemoService.saveDesc(testDemo));
    }

    /**
     * 查询列表
     *
     * @return
     */
    @GetMapping("/list")
    public WebResult<List<TestDemo>> list() {
        List<TestDemo> testDemos = testDemoService.selectAll();
        return WebResult.okResult(testDemos);
    }

    /**
     * 多条件查询
     *
     * @param request
     * @return
     */
    @PostMapping("/page")
    public WebResult<Page> page(@RequestBody @Validated DemoTestRequest request) {
        Pageable pageable = PageRequest.of(request.pageNumber, request.pageSize);
        DemoTestCriteria criteria = new DemoTestCriteria();
        BeanUtil.copyProperties(request, criteria, CopyOptions.create().setIgnoreNullValue(true));
        Page<TestDemo> allPage = testDemoService.findAllPage(criteria, pageable);
        return WebResult.okResult(allPage);
    }

    /**
     * 测试 querydsl 查询
     *
     * @param address
     * @param name
     * @return
     */
    @GetMapping("/query")
    public WebResult<List<TestDemo>> query(String address, String name) {
        return WebResult.okResult(testDemoService.findByConditions(address, name));
    }


    /**
     * 测试 querydsl 查询
     *
     * @param address
     * @param name
     * @return
     */
    @GetMapping("/queryPage")
    public WebResult<Page<TestDemo>> query(String address, String name, int page, int size) {
        PageRequest pageRequest = PageRequest.of(page, size);
        return WebResult.okResult(testDemoService.demoPage(address, name, pageRequest));
    }
}
